﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using System.ServiceModel;
using Architects.Dominio;
using MvcAppCondominio.ViewData;
using MvcAppCondominio.ResidenteProxy;

namespace MvcAppCondominio.Controllers
{
    public class ResidenteController : Controller
    {
        ResidenteServiceClient client = new ResidenteServiceClient();

        public ActionResult Index(int? page)
        {
            var viewdata = new ResidenteViewData();
            int currentPage = page ?? 0;
            viewdata.Residentes = client.ListarResidentesPaginado(currentPage, 10);
            viewdata.NextPage = currentPage + 1;
            viewdata.PreviousPage = (currentPage <= 0) ? 0 : currentPage - 1;

            return View(viewdata);
        }        

        public ActionResult Details(int id)
        {
            var residente = client.ObtenerResidentePorID(id);
            return View(residente);            
        }
                
        public ActionResult Create()
        {
            ResidenteViewData residenteViewData = new ResidenteViewData();
            return View(residenteViewData);
        } 

        
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                ResidenteViewData residenteViewData = new ResidenteViewData();
                UpdateModel(residenteViewData);
                client.CrearResidente(residenteViewData.Residente);
                ViewData["Message"] = "";
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                ViewData["Message"] = ex.Detail.ValidationError;
                return View();
            }
        }
        
        
        public ActionResult Edit(int id)
        {
            ResidenteViewData residenteViewData = new ResidenteViewData();
            residenteViewData.Residente = client.ObtenerResidentePorID(id);
            return View(residenteViewData);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                ResidenteViewData residenteViewData = new ResidenteViewData();
                residenteViewData.Residente = client.ObtenerResidentePorID(id);
                UpdateModel(residenteViewData);
                client.ActualizarResidente(residenteViewData.Residente);
                ViewData["Message"] = "";
 
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                ViewData["Message"] = ex.Detail.ValidationError;
                return View();
            }
        }
        
        public ActionResult Delete(int id)
        {
            client.EliminarResidente(id);            
            return View();
        }

        
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {    
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
